package com.showsu.editor.entity.tikhub;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.showsu.editor.entity.platform.xhs.XhsAwemeCommentInfo;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.lang.reflect.Type;

/**
 * @Author: Jhonplue
 * @Description: Xhs作品评论列表
 * @Date: Created in 16:09 下午 2023/8/28
 * @Modified By:
 */
@Data
public class TikhubXhsAwemeCommentListData implements Serializable {

    /**
     * 结果码
     */
    private Integer code;
    /**
     * 结果码
     */
    private Integer status_code;
    /**
     * 请求结果状态
     */
    private Boolean success;

    /**
     * 提示信息
     */
    private String msg;

    /**
     * 错误结果码
     */
    private Integer errcode;

    /**
     * 错误提示信息
     */
    private String errmsg;

    /**
     * xhs作品评论列表数据
     */
    private TikhubXhsAwemeCommentListDataData data;

    public void initVideoInfo() {
        //实际调试的时候发现接口返回的video_info是有转义符的字符串，需要单独处理成对象
        if (this.data != null && ! CollectionUtils.isEmpty(this.data.getComments())) {
            Type typeVideoInfo = new TypeToken<XhsAwemeCommentInfo.Image.VideoInfo>(){}.getType();
            Gson gson = new Gson();
            this.data.getComments().forEach(comment -> {
                if (! CollectionUtils.isEmpty(comment.getPictures())) {
                    comment.getPictures().forEach(picture -> {
                        if (StringUtils.isNotBlank(picture.getVideo_info())) {
                            picture.setVideoInfo(gson.fromJson(picture.getVideo_info(), typeVideoInfo));
                        }
                    });
                }
            });
        }
    }
}
